<?php

!defined('IN_HDWIKI') && exit('Access Denied');

class control extends base{

	function control(& $get,& $post){
		$this->base( & $get,& $post);
		$this->load('category');
		$this->load('doc');
	}

	function dodefault(){
		$this->doview();
	}

	function doview(){
		$allcategory=$_ENV['category']->get_category_cache();
		if(!is_numeric($this->get[2])){
			$subcategory=$_ENV['category']->get_site_category(0,$allcategory);
			$this->view->assign('navtitle',$this->view->lang['wikiCategory'].'-');
			$this->view->assign('subcategory',$subcategory);
			$this->view->display("categorylist");
		}else{
			$category=$_ENV['category']->get_category($this->get[2]);
			if(!(bool)$category){
				$this->message($this->view->lang['categoryNotExist'],'BACK',0);
			}
			$page = max(1, intval($this->get[3]));
			$start_limit = ($page - 1) * $this->setting['category_view'];
		
			$cids=$this->get[2].$_ENV['category']->get_all_subcate($this->get[2],$allcategory);
			$count=$_ENV['doc']->get_totalnum_by_cid($cids);
			$list=$_ENV['doc']->get_docs_by_cid($cids,$start_limit,$this->setting['category_view']);
			$departstr=$this->multi($count, $this->setting['category_view'], $page,'category-view-'.$this->get[2]);
			
			$subcategory=$_ENV['category']->get_site_category($this->get[2],$allcategory);
			$category['navigation']=unserialize($category['navigation']);
			$this->view->assign('list',$list);
			$this->view->assign('navtitle',$category['name'].'-');
			$this->view->assign('subcategory',$subcategory);
			$this->view->assign('category',$category);
			$this->view->assign('departstr',$departstr);
			$this->view->display("category");
		}
	}

	function doletter(){
		$allcategory=$_ENV['category']->get_category_cache();
		if(0==$this->get[2]||!preg_match("/^[\w]$/i",$this->get[3])){
			$this->message($this->view->lang['parameterError'],'BACK',0);
		}else{
			$category=$_ENV['category']->get_category($this->get[2]);
			if(!(bool)$category){
				$this->message($this->view->lang['categoryNotExist'],'BACK',0);
			}
			$page = max(1, intval($this->get[4]));
			$start_limit = ($page - 1) * $this->setting['category_letter'];
			
			$count=$_ENV['doc']->get_totalnum_by_cid($this->get[2],$this->get[3]);
			$list=$_ENV['doc']->get_docs_by_cid($this->get[2],$start_limit,$this->setting['category_letter'],$this->get[3]);			
			$departstr=$this->multi($count, $this->setting['category_letter'], $page,'category-letter-'.$this->get[2].'-'.$this->get[3]);
			$subcategory=$_ENV['category']->get_site_category($this->get[2],$allcategory);
			$category['navigation']=unserialize($category['navigation']);
			$this->view->assign('list',$list);
			$this->view->assign('subcategory',$subcategory);
			$this->view->assign('category',$category);
			$this->view->assign('letter',strtoupper($this->get[3]));
			$this->view->assign('departstr',$departstr);
			$this->view->display("category");
		}
	}

	function doajax(){
		$content = '<dl>';
		$catid = isset($this->get[2])?$this->get[2]:0;
		$catid = intval($catid);
		$cat = $_ENV['category']->get_category($catid);
		$content .= '<dt class="mar-t8 r"><span  class="r hand font14 blue"><a href="javascript:catevalue.removeCateTree()">'.$this->view->lang['categoryCloseImg'].'</a></span></dt>';
		$cats = $_ENV['category']->get_subcate($catid);
		if(is_array($cats))	{
			if($catid!=0)$content .= '<dd><a href="javascript:catevalue.cateOk('.$cat['cid'].',\''.$cat['name'].'\')">'.$cat['name'].'</a></dd><br></br>';
			foreach($cats as $p){
				if($_ENV['category']->get_subcate($p['cid'])){
					$img = '<input onclick="javascript:catevalue.ajax('.$p['cid'].')" type="image" src="style/'.$this->setting['style_name'].'/add.gif"/>';
					$content .= '<dd>'.$img.'<a href="javascript:catevalue.ajax('.$p['cid'].')">'.$p['name'].'</a></dd>';
				}else{
					 $img = $img = '<input onclick="javascript:void(0)" type="image" src="style/'.$this->setting['style_name'].'/no_next.gif"/>';
					$content .= '<dd>'.$img.'<a href="javascript:catevalue.cateOk('.$p['cid'].',\''.$p['name'].'\')">'.$p['name'].'</a></dd>';
				}
			}
		}else{
			$content .= '<dd>'.$this->view->lang['categoryNotExist'].'</dd>';
		}
		$content .= '<dt class="mar-t8 r bor-gray-das"><a href="javascript:catevalue.ajax('.$cat['pid'].')" class="r"><img alt="" src="style/'.$this->setting['style_name'].'/up.jpg" class="tag_help"/>'.$this->view->lang['categoryUpLevel'].'</a><a href="javascript:catevalue.ajax(0)" class="r">'.$this->view->lang['categoryTopLevel'].'</a></dt></dl>';
		$this->message($content,'',2);
	}
}
?>